package com.ciaojian.core.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ciaojian.core.mapper.ChargeUserMapper;
import com.ciaojian.core.model.ChargeLog;
import com.ciaojian.core.model.ChargeUser;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author oxiny
 */
public abstract class ChargeUserService extends ServiceImpl<ChargeUserMapper, ChargeUser> {
    /**
     * 查询用户余额
     *
     * @param userId 用户ID
     * @return 用户余额
     */
    public Integer getBalance(Integer userId) {
        return baseMapper.getBalanceByUserId(userId);
    }

    /**
     * 查询用户冻结余额
     *
     * @param userId /
     * @return /
     */
    public Integer getFrozenBalance(Integer userId) {
        return baseMapper.getFrozenBalanceByUserId(userId);
    }

    /**
     * 变更余额
     *
     * @param userId     用户ID
     * @param amount     数额
     * @param dir        方向 0支出 1收入
     * @param targetType 来源类型  0充值 1订单 2冻结 3解冻 4扣除
     * @param targetId   来源ID
     * @return 成功返回TRUE
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean chargeChange(Integer userId,
                                Integer amount,
                                Integer dir,
                                Integer targetType,
                                Integer targetId) {
        // 版本号
        Integer version = baseMapper.getVersionByUserId(userId);
        // 余额
        Integer balance = baseMapper.getBalanceByUserId(userId);
        if (baseMapper.change(userId, amount, dir, version)) {
            // 添加记录
            ChargeLog chargeLog = new ChargeLog();
            chargeLog.setUserId(userId);
            chargeLog.setTargetType(targetType);
            chargeLog.setTargetId(targetId);
            chargeLog.setPrice(amount);
            chargeLog.setBefBalance(amount);
            chargeLog.setAftBalance(balance + amount);
            chargeLog.insert();
            return true;
        } else {
            // 失败
            return false;
        }
    }

    /**
     * 冻结余额
     *
     * <p>
     * 1.冻结balance to frozenBalance
     * 2.解冻frozenBalance to balance
     * 3.直接扣除frozenBalance
     * </p>
     *
     * @param userId     用户ID
     * @param amount     余额
     * @param targetType 来源类型
     * @param targetId   来源ID
     * @param dir        方向 1.冻结balance to frozenBalance 2.解冻frozenBalance to balance 3.直接扣除frozenBalance
     * @return 成功返回TRUE
     */
    public Boolean frozenCharge(Integer userId,
                                Integer amount,
                                Integer targetType,
                                Integer targetId,
                                Integer dir) {
        // 版本号
        Integer version = baseMapper.getVersionByUserId(userId);
        // 余额
        Integer balance = baseMapper.getBalanceByUserId(userId);
        // 冻结余额
        Integer frozenBalance = baseMapper.getFrozenBalanceByUserId(userId);
        if (baseMapper.frozen(userId, amount, version)) {
            // 添加记录
            ChargeLog chargeLog = new ChargeLog();
            if (dir == 1) {
                chargeLog.setAftBalance(balance - amount);
            } else if (dir == 2) {
                chargeLog.setAftBalance(balance + amount);
            } else if (dir == 3) {
                chargeLog.setAftBalance(balance);
            }
            chargeLog.setBefBalance(balance);
            chargeLog.setUserId(userId);
            chargeLog.setTargetType(targetType);
            chargeLog.setTargetId(targetId);
            chargeLog.setPrice(amount);
            chargeLog.setBefBalance(amount);
            chargeLog.setAftBalance(frozenBalance + amount);
            chargeLog.insert();
            return true;
        } else {
            return false;
        }
    }
}
